﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ceTe.DynamicPDF;
using ceTe.DynamicPDF.PageElements;
using ceTe.DynamicPDF.Text;

namespace ToppingControl.Models
{
   public class clsPrintPDF
   {
      public const float PAGE_WIDTH = 495;

      public const float PAGE_HEIGHT = 742;
      public const float FONT_SIZE_HEAD = 14f;
      public const float FONT_SIZE_NORMAL = 12f;
      public const float FONT_SIZE_SMALL = 11f;

      public const float FONT_SIZE_VERY_SMALL = 10f;

      public const float COLUMN_HEIGHT = 10f;
      public static readonly TrueTypeFont FONT_NORMAL = new TrueTypeFont("CORDIA.TTF");
      public static readonly TrueTypeFont FONT_BOLD = new TrueTypeFont("CORDIAB.TTF");
      public static readonly TrueTypeFont FONT_UNDERLINE = new TrueTypeFont("CORDIAB.TTF");
      public static readonly TrueTypeFont FONT_ITALIC = new TrueTypeFont("CORDIAI.TTF");
      public static readonly TrueTypeFont FONT_BOLD_ITALIC = new TrueTypeFont("CORDIAZ.TTF");

      public static Page NewPagePDF(PageOrientation PageOrientation = PageOrientation.Portrait)
      {
         // Create a page dimensions object and set the margins
         ceTe.DynamicPDF.PageDimensions dimensions = default(ceTe.DynamicPDF.PageDimensions);
         dimensions = new ceTe.DynamicPDF.PageDimensions(PageSize.A4, PageOrientation);
         dimensions.BottomMargin = 20;
         dimensions.TopMargin = 35;
         dimensions.LeftMargin = 35;
         dimensions.RightMargin = 10;

         // Create a page using a page dimensions object and add it to the document
         return new Page(dimensions);
      }

      /**####### PAGE ############**/
      public static void AddPagesGetOverflowRows(Document doc, Page p, Table tbl)
      {
         while (tbl != null)
         {
            p.Elements.Add(tbl);
            doc.Pages.Add(p);
            p = NewPagePDF();
            tbl = tbl.GetOverflowRows(0, 0);
            if (tbl != null)
               tbl.Height = tbl.GetRequiredHeight();
         }
      }


      public static void AddPagesGetOverflowRows(float x, float y, Document doc, Page p, Table tbl)
      {
         while (tbl != null)
         {
            p.Elements.Add(tbl);
            doc.Pages.Add(p);
            p = NewPagePDF();
            tbl = tbl.GetOverflowRows(x, y);
            if (tbl != null)
               tbl.Height = tbl.GetRequiredHeight();
         }
      }

      /**####### ROWS ############**/
      public static Row CreateNewRowHeader(Table tbl)
      {
         return tbl.Rows.Add(COLUMN_HEIGHT, FONT_BOLD, FONT_SIZE_NORMAL);
      }

      public static Row CreateNewRowData(Table tbl)
      {
         return tbl.Rows.Add(COLUMN_HEIGHT, FONT_NORMAL, FONT_SIZE_NORMAL);
      }
      
      /**####### COL ############**/
      public static void AddColumnWithPercent(Table tbl, float percent)
      {
         tbl.Columns.Add(tbl.Width / 100 * percent);
      }

      public static void AddColumn(Table tbl, float width)
      {
         tbl.Columns.Add(width);
      }

      /**####### TBL ############**/
      public static void SetTableHeader(Table tbl, int RepeatColumnHeaderCount, int RepeatRowHeaderCount)
      {
         tbl.RepeatColumnHeaderCount = RepeatColumnHeaderCount;
         tbl.RepeatRowHeaderCount = RepeatRowHeaderCount;
      }

      public static void SetTableBorder(Table tbl)
      {
         tbl.BorderWidth = 0.5f;
      }

      public static void SetTableBorder(Table tbl, float BorderWidth)
      {
         tbl.BorderWidth = BorderWidth;
      }

      public static void SetTableBorder(Table tbl, float BorderWidth, ceTe.DynamicPDF.Color BorderColor)
      {
         tbl.BorderWidth = BorderWidth;
         tbl.BorderColor = BorderColor;
      }

      public static void SetTableNoLine(Table tbl)
      {
         tbl.BorderWidth = 0;
      }

      public static void SetTableNoLine(Table tbl, float BorderWidth)
      {
         tbl.BorderWidth = BorderWidth;
         tbl.BorderColor = Grayscale.White;
      }

   }
}